package com.gulimall.seckill.scheduled;

import com.gulimall.seckill.service.SecKillService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 定时任务开启@EnableScheduling
 *
 *
 *
 * 异步任务开启@EnableAsync
 *      EnableAsync 开启异步功能
 *      Async 使用异步调用该方法
 */
@EnableScheduling
@Component
@EnableAsync
public class ScheduledTest {

    @Autowired
    private SecKillService secKillService;

    @Autowired
    private RedissonClient redissonClient;

    private String PREFIX = "upload:lock";

    @Async
    @Scheduled(cron = "30 * * * * ? ")
    public void upLoadSecKillProduct() {
        RLock lock = redissonClient.getLock(PREFIX);

        lock.lock(10, TimeUnit.SECONDS);
        try {
            secKillService.upLoadSecKillProduct();
        } finally {
            lock.unlock();
        }
    }

}
